package com.supermarket.web.controller.supermarket;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.supermarket.business.dto.SupermarketSalesOrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.supermarket.common.annotation.Log;
import com.supermarket.common.core.controller.BaseController;
import com.supermarket.common.core.domain.AjaxResult;
import com.supermarket.common.enums.BusinessType;
import com.supermarket.business.domain.SupermarketSalesOrder;
import com.supermarket.business.service.ISupermarketSalesOrderService;
import com.supermarket.common.utils.poi.ExcelUtil;
import com.supermarket.common.core.page.TableDataInfo;

/**
 * 销售订单Controller
 *
 * @author ruoyi
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/supermarket/order")
public class SupermarketSalesOrderController extends BaseController
{
    @Autowired
    private ISupermarketSalesOrderService supermarketSalesOrderService;

    /**
     * 查询销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupermarketSalesOrderDTO supermarketSalesOrderDTO)
    {
        startPage();
        SupermarketSalesOrder supermarketSalesOrder = new SupermarketSalesOrder();
        BeanUtils.copyProperties(supermarketSalesOrderDTO, supermarketSalesOrder);
        List<SupermarketSalesOrder> list = supermarketSalesOrderService.selectSupermarketSalesOrderList(supermarketSalesOrder);
        return getDataTable(list);
    }

    /**
     * 导出销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:export')")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupermarketSalesOrder supermarketSalesOrder)
    {
        List<SupermarketSalesOrder> list = supermarketSalesOrderService.selectSupermarketSalesOrderList(supermarketSalesOrder);
        ExcelUtil<SupermarketSalesOrder> util = new ExcelUtil<SupermarketSalesOrder>(SupermarketSalesOrder.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    /**
     * 获取销售订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(supermarketSalesOrderService.selectSupermarketSalesOrderByOrderId(orderId));
    }

    /**
     * 新增销售订单
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:add')")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupermarketSalesOrderDTO supermarketSalesOrderDTO)
    {
        SupermarketSalesOrder supermarketSalesOrder = new SupermarketSalesOrder();
        BeanUtils.copyProperties(supermarketSalesOrderDTO, supermarketSalesOrder);
        return toAjax(supermarketSalesOrderService.insertSupermarketSalesOrder(supermarketSalesOrder));
    }

    /**
     * 修改销售订单
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:edit')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupermarketSalesOrderDTO supermarketSalesOrderDTO)
    {
        SupermarketSalesOrder supermarketSalesOrder = new SupermarketSalesOrder();
        BeanUtils.copyProperties(supermarketSalesOrderDTO, supermarketSalesOrder);
        return toAjax(supermarketSalesOrderService.updateSupermarketSalesOrder(supermarketSalesOrder));
    }

    /**
     * 删除销售订单
     */
    @PreAuthorize("@ss.hasPermi('supermarket:order:remove')")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(supermarketSalesOrderService.deleteSupermarketSalesOrderByOrderIds(orderIds));
    }
}
